#include "../mqserver/exchange.hpp"

#include <gtest/gtest.h>

mymq::ExchangeManager::ptr emp;

class EchangeTest: public testing::Environment
{
    virtual void SetUp() override
    {
        emp = std::make_shared<mymq::ExchangeManager>("./data/meta.db");
    }
    virtual void TearDown() override
    {
        // emp->clear();
        std::cout << "完成资源的清理化工作" << std::endl;
    }
};

TEST(exchange_test, insert_test)
{
    std::unordered_map<std::string, std::string> args = {{"k2", "v2"}, {"k1", "v1"}};
    emp->DeclareExchange("exchange1", mymq::ExchangeType::DIRECT, true, false, args);
    emp->DeclareExchange("exchange2", mymq::ExchangeType::DIRECT, true, false, args);
    emp->DeclareExchange("exchange3", mymq::ExchangeType::DIRECT, true, false, std::unordered_map<std::string, std::string>());
    emp->DeclareExchange("exchange4", mymq::ExchangeType::DIRECT, true, false, args);
    ASSERT_EQ(emp->Size(), 4);
}

TEST(exchange_test, select_test)
{
    ASSERT_EQ(emp->Exists("exchange1"), true);
    mymq::Exchange::ptr exp = emp->SelectExhcange("exchange3");
    ASSERT_EQ(exp->name, "exchange3");
    ASSERT_EQ(exp->durable, true);
    ASSERT_EQ(exp->auto_delete, false);
    ASSERT_EQ(exp->type, mymq::ExchangeType::DIRECT);
    ASSERT_EQ(exp->GetArgs(), "");
}

TEST(exchange_test, delete_test)
{
    emp->DeleteExchange("exchange1");
    ASSERT_EQ(emp->Exists("exchange1"), false);
}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new EchangeTest);
    RUN_ALL_TESTS();

    return 0;
}